package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.ClueDialogTitleAdp;
import com.baihan.angus.app.game.common.bean.entity.MapEntity;
import com.baihan.angus.app.game.common.util.ImageLoader;

import java.util.List;
import java.util.stream.Collectors;

public class MapDialog extends Dialog {

    private Context mContext;
    private Callback mCallback;
    private WindowManager.LayoutParams mLayoutParams;
    private TextView tvTitle;
    private ImageView ivClose;
    private ImageView ivMap;
    private RecyclerView rvMapTitle;
    private LinearLayout llNoData, llTitle, llContent;
    private View viewDivider;

    private ClueDialogTitleAdp mMapDialogTitleAdp;
    private List<MapEntity> mapList;
    private List<String> titleList;
    private String title;
    private int selectPosition = 0;

    public MapDialog(@NonNull Context context, Callback mCallback) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        this.mCallback = mCallback;
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        mLayoutParams.height = (int) (AppUtil.getScreenHeight(context) * 0.8);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_map, (ViewGroup) null);
        setContentView(dialogView);
        tvTitle = dialogView.findViewById(R.id.tvTitle);
        rvMapTitle = dialogView.findViewById(R.id.rvMapTitle);
        ivClose = dialogView.findViewById(R.id.ivClose);
        ivMap = dialogView.findViewById(R.id.ivMap);
        llNoData = dialogView.findViewById(R.id.llNoData);
        llContent = dialogView.findViewById(R.id.llContent);
        llTitle = dialogView.findViewById(R.id.llTitle);
        viewDivider = dialogView.findViewById(R.id.viewDivider);

        mMapDialogTitleAdp = new ClueDialogTitleAdp();
        rvMapTitle.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        mMapDialogTitleAdp.bindToRecycleView(rvMapTitle);

        mMapDialogTitleAdp.setOnItemClickListener(new ClueDialogTitleAdp.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                selectPosition = position;
                mMapDialogTitleAdp.setThisPosition(position);
                mMapDialogTitleAdp.notifyDataSetChanged();
                title = mapList.get(position).getTitle();
                tvTitle.setText(title);
                if (null != mapList && mapList.size() > 0) {
                    ImageLoader.loadImage(mContext, ivMap, mapList.get(position).getUrl().get(0).getUrl(), R.mipmap.ic_test_bg, 0);
                    llContent.setVisibility(View.VISIBLE);
                    llNoData.setVisibility(View.GONE);
                } else {
                    llContent.setVisibility(View.GONE);
                    llNoData.setVisibility(View.VISIBLE);
                }
            }
        });

        ivMap.setOnClickListener(v -> {
            if (null != mCallback) {
                mCallback.onShowMap(mapList.get(selectPosition));
            }
        });

        ivClose.setOnClickListener(v -> {
            cancel();
        });

    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void initData(List<MapEntity> list) {
        this.mapList = list;
        mMapDialogTitleAdp.clear();
        if (null != mapList && mapList.size() > 0) {
            llContent.setVisibility(View.VISIBLE);
            llTitle.setVisibility(View.VISIBLE);
            viewDivider.setVisibility(View.VISIBLE);
            llNoData.setVisibility(View.GONE);

            //该方法6.0不兼容
            titleList = mapList.stream().map(MapEntity::getTitle).collect(Collectors.toList());
            mMapDialogTitleAdp.addAll(titleList);
            selectPosition = 0;
            title = mapList.get(selectPosition).getTitle();
            tvTitle.setText(title);
            if (null != mapList && mapList.size() > 0) {
                ImageLoader.loadImage(mContext, ivMap, mapList.get(selectPosition).getUrl().get(0).getUrl(), R.mipmap.ic_test_bg, 0);
                llContent.setVisibility(View.VISIBLE);
                llNoData.setVisibility(View.GONE);
            } else {
                llContent.setVisibility(View.GONE);
                llNoData.setVisibility(View.VISIBLE);
            }
            mMapDialogTitleAdp.setThisPosition(selectPosition);
        } else {
            llContent.setVisibility(View.GONE);
            llTitle.setVisibility(View.GONE);
            viewDivider.setVisibility(View.GONE);
            llNoData.setVisibility(View.VISIBLE);
        }
    }

    public interface Callback {
        void onShowMap(MapEntity mapData);
    }
}
